import { Application } from 'egg';
import * as pageFindPlugin from 'mongoose-paginate';

export default (app: Application) => {
  const mongoose = app.mongoose;
  const ObjectId = mongoose.Schema.Types.ObjectId;
  const schema = new mongoose.Schema({
    // 名称
    name: {
      type: String,
      required: true,
    },
    // 图片
    imgUrl: {
      type: String,
      required: true,
    },
    // 类型, 对应banner_type集合
    type: {
      type: String,
      ref: 'Bannertype',
      required: true,
    },
    // 渠道
    channel: {
      type: String,
      required: true,
      enum: [ 'app', 'h5' ],
      default: 'app', // app | h5
    },
    // 资源路径
    uri: {
      type: String,
      required: true,
    },
    // 门店
    storeIds: {
      type: [ ObjectId ],
      default: [],
      required: false,
    },
    // 是否所有门店生效，为false时，匹配storeIds
    isAllStore: {
      type: Boolean,
      required: true,
    },
    // 排序(不能重复)
    sort: {
      type: Number,
      required: true,
    },
    // 状态
    status: {
      type: Number,
      required: true,
      enum: [ 0, 1, 2 ],
      default: 0, // 0: 上架, 1: 下架, 2: 删除
    },
  });

  schema.index({ name: 1, type: 1, status: 1, channel: 1 });
  schema.plugin(pageFindPlugin, {});
  return mongoose.model('Banner', schema);
};
